package com.frxuexi.shop2307.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.lib_base.utils.ArouterService
import com.frxuexi.lib_base.utils.GlideUtils
import com.frxuexi.lib_base.utils.LiveDataBus
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.databinding.FragmentMineBinding
import com.frxuexi.shop2307.user.UserViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MineFragment : BaseFragment<FragmentMineBinding, UserViewModel>() {
    override fun initData() {
        LiveDataBus.with("pic",String::class.java).observe(this){
            GlideUtils.getCircleImage(binding.mineIv,it)
        }

        vm.infoData.observe(this){
            binding.mineName.setText(it.nickname)
            GlideUtils.getCircleImage(binding.mineIv,it.avatar)
        }

    }

    override fun initView() {


        binding.mineName.setOnClickListener{
            if(SPUtils.getInstance().getString(Const.TOKEN,"").isNullOrEmpty()){
                ARouter.getInstance().build("/app/LoginActivity").navigation()
            }else {
                ARouter.getInstance().build("/app/UpdateInfoActivity").navigation(requireContext(),ArouterService())
            }
        }



        binding.mineIv.setOnClickListener {
            PictureSelector.create(requireActivity(),PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.mineBtnchong.setOnClickListener {
            ArouterManager.nav(requireContext(),"/app/MyMoneyActivity")
        }

        binding.mineJi.setOnClickListener {
            ArouterManager.nav(requireContext(),"/app/MyScoreActivity")
        }


    }

    override fun onResume() {
        super.onResume()

        vm.getInfo()
        SPUtils.getInstance().apply {
//                获取原来的总金额
            val m=getFloat("money",0.0f)
            binding.mineSum.setText("￥${m}")
            binding.mineChongmoney.setText("￥${m}")
        }
        /*val name = SPUtils.getInstance().getString(Const.NAME, "")
        if (name.isNotEmpty()) {
            binding.mineName.setText(name)
        }*/
    }

}